/**
 * @description: 全员营销活动列表页
 *
 * @author Yangcl
 * @date 2018年6月22日 上午10:35:27
 * @version 1.0.0.1
 */
var aemInfo = {
		myDataTable : null,  
		path : "",        // 绝对路径
		cid : null,
		// 搜索条件
		name : "",
		createUserName : "",
		aemTypeId : null ,
		status : null,
		trDialog : null,
		
		init:function(cid_){
			aemInfo.cid = cid_;
			aemInfo.path = aemInfo.getPath();       // 绝对路径
			aemInfo.aemTypeSelect();  
			return aemInfo;
		},
		
		dataTable : function(){
			aemInfo.myDataTable = $('#page-list-table').DataTable({
				columns: [
	         		{"data":"name"},		// 活动名称
	         		// {"data":"sharePeople"},	// 分享人 / 次   本期不做
	         		{"data":"uv"},   // uv/pv
	         		{"data":"status"}, // 活动状态 
	         		{"data":"createTimeString"},   // 创建时间
	         		{"data":"userName"}, 	 // 创建者
	         		{"data":"aemTypeName"}, 	 // 活动类型
	         		{"data":"id"}, 	 // 操作  此处需要render操作。render: function(data, type, full)   targets: 7
	         	],
	         	
	         	ajax:{
	     	    	contentType: 'application/json;charset=utf-8',
	     	    	type: 'POST',
	     	    	url: aemInfo.path + "/aemInfo/ajaxAemInfoPageList",
	     	    	data: function(e){
	     	    		e.name = aemInfo.name;
	     	    		e.createUserName = aemInfo.createUserName;
	     	    		e.aemTypeId = aemInfo.aemTypeId;
	     	    		e.status = aemInfo.status;
	     				return JSON.stringify(e); 
	     			}
	     		},
	         	
	     		processing: true,
	     		ordering: false,
	     		searching: false,
	     		info: true,
	     		autoWidth: true,
	     		lengthChange: false,
	     		"order": [[ 2, "desc" ]],
	     	    language: {
	     	    	emptyTable: '没有数据',
	     	    	info: '从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录',
	     			infoEmpty: '没有数据',
	     			lengthMenu: '每页显示 _MENU_ 条记录',
	     			loadingRecords: '正在加载数据....',
	     			processing: '正在处理数据....',
	     			zeroRecords: '没有找到匹配的记录',
	     			paginate: {
	     				first: '首页',
	     				previous: '前页',
	     				next: '后页',
	     				last: '尾页'
	     			}
	     	    },
	     	    order: [[1,'desc']],
	     		pageLength: 10,
	     	    pagingType: 'full_numbers',
	     	    columnDefs: [
	     	    	{
		     			render: function(data, type, e) { 
		     				var zid = e.id;
		                    var  result = '<a id="td-span-' + zid + '" style="padding-top:6px;padding-bottom:6px;z-index:999;" onmousemove ="aemInfo.showDialog(' 
		                    	+ e.countShareLevel + ','  + e.clickSore + ','  + e.shareScore + ',\''  + zid + '\')">' + e.name + '</a>';   
		     				return result;
		                 },
		     			targets: 0
	     	    	},
	     	    	/*{
		     			render: function(data, type, e) { 
		                    var  result = e.sharePeople + '/' + e.shareCount;  
		     				return result;
		                 },
		     			targets: 1
	     	    	},*/
	     	    	{
		     			render: function(data, type, e) {
		                    var result = e.uv + '/' + e.pv; 
		     				return result;
		                 },
		     			targets: 1
	     	    	},
	     	    	{
		     			render: function(data, type, e) {
		                    var result = ''; 
		                    if(e.status == "1"){
		                    	result = '未开始'; 
		                    }else if(e.status == "2"){
		                    	result = '进行中'; 
		                    }else{
		                    	result = '已结束';                             
		                    }
		     				return result;
		                 },
		     			targets: 2
	     	    	},
	     	    	{
		     			render: function(data, type, e) {
		                    if(e.aemTypeName == "" || typeof(e.aemTypeName) == "undefined" || e.aemTypeName == null){
		                    	return '未分类'; 
		                    } 
		     				return e.aemTypeName;
		                 },
		     			targets: 5
	     	    	},
	     	    	{
		     			render: function(data, type, e) {
		     				e.url = aemInfo.path + "material/mtaerialPreviewPage?id=" + e.mgiId + "&cid=" + e.cid + "&materialId=" + e.mgiId;     // 不跳转静态页，跳转到素材预览页
		     				var result = ''; 
		                    if(e.status == "1"){
		                    	result += '<a href="javascript:void(0);"><font color="#686B6D">活动统计</font></a> &nbsp&nbsp&nbsp&nbsp'; 
			                    result += '<a href="javascript:void(0);"><font color="#686B6D">活动传播</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);"><font color="#686B6D">参与名单</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" attr="' + e.id + '" attr-status="' + e.status + '" onclick="aemInfo.aemInfoEditPage(this)"><font color="#1ab394">编辑</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" id="sq-' + e.id + '" attr="' + e.url + '" onclick="aemInfo.showQrcode(this)"><font color="#1ab394">预览</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" attr="' + e.id + '" onclick="aemInfo.aemInfoDelete(this)"><font color="#1ab394">删除</font></a>';
		                    }else if(e.status == "2"){
		                    	if(e.todayAem == "current"){
		                    		result += '<a href="javascript:void(0);"><font color="#686B6D">活动统计</font></a> &nbsp&nbsp&nbsp&nbsp'; 
		                    	}else{
		                    		result += '<a href="javascript:void(0);" attr-id="' + e.id + '" attr-name="' + e.name + '" attr-ctime="' + e.createTimeString + '" onclick="aemInfo.aemStatisticPage(this)"><font color="#1ab394">活动统计</font></a> &nbsp&nbsp&nbsp&nbsp'; 
		                    	}
			                    result += '<a href="javascript:void(0);" attr-id="' + e.id + '"attr-name="' + e.name + '" onclick="aemInfo.aemDisseminationPage(this)"><font color="#1ab394">活动传播</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" attr="' + e.id + '" attr-status="' + e.status + '" attr-s="' + e.startTime + '" attr-e="' + e.endTime + '" onclick="aemInfo.aemJoinPageList(this)"><font color="#1ab394">参与名单</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" attr="' + e.id + '" attr-status="' + e.status + '"  onclick="aemInfo.aemInfoEditPage(this)"><font color="#1ab394">编辑</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" id="sq-' + e.id + '" attr="' + e.url + '" onclick="aemInfo.showQrcode(this)"><font color="#1ab394">预览</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);"><font color="#686B6D">删除</font></a>';
		                    }else{
		                    	if(e.todayAem == "current"){
		                    		result += '<a href="javascript:void(0);"><font color="#686B6D">活动统计</font></a> &nbsp&nbsp&nbsp&nbsp'; 
		                    	}else{
		                    		result += '<a href="javascript:void(0);" attr-id="' + e.id + '" attr-name="' + e.name + '" attr-ctime="' + e.createTimeString + '" onclick="aemInfo.aemStatisticPage(this)"><font color="#1ab394">活动统计</font></a> &nbsp&nbsp&nbsp&nbsp'; 
		                    	}
			                    result += '<a href="javascript:void(0);" attr-id="' + e.id + '"attr-name="' + e.name + '" onclick="aemInfo.aemDisseminationPage(this)"><font color="#1ab394">活动传播</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" attr="' + e.id + '" attr-status="' + e.status + '" attr-s="' + e.startTime + '" attr-e="' + e.endTime + '" onclick="aemInfo.aemJoinPageList(this)"><font color="#1ab394">参与名单</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" attr="' + e.id + '" attr-status="' + e.status + '" onclick="aemInfo.aemInfoEditPage(this)"><font color="#1ab394">编辑</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);" id="sq-' + e.id + '" attr="' + e.url + '" onclick="aemInfo.showQrcode(this)"><font color="#1ab394">预览</font></a> &nbsp&nbsp&nbsp&nbsp';
			                    result += '<a href="javascript:void(0);"><font color="#686B6D">删除</font></a>';                      
		                    }
		     				return result;
		                 },
		     			targets: 6
	     	    	}
     	    	],
	     	    serverSide: true,
	     	    "initComplete": function(settings, json) {
	     	    	/*$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});
	     	    	var info = aemInfo.myDataTable.page.info(); 
	                 if(json.error_code=="20028"){
	                 	loginDialog();
	                 }else{
	                 }     	    	
	                 imageLoadError();*/
	     	    }
	     	});
			
			return aemInfo.myDataTable; 
		},
		
		// 搜索
		search : function(){
			aemInfo.name = $("#aem-name").val();
    		aemInfo.createUserName = $("#aem-create-user").val();
    		aemInfo.aemTypeId = $("#aem-type").val();
    		aemInfo.status = $("#aem-status").val();
			aemInfo.myDataTable.ajax.reload(function(){});
		},
		
		// 初始化下拉框
		aemTypeSelect : function(){
			var data = null;
			var url = aemInfo.path + "/aemInfo/ajaxAemTypeList"; 
			var e = JSON.parse(aemInfo.syncAjax("post", url , data , false)); 
			if(e.status == "200"){
				var arr = e.list;
				var html_ = '<option value="">请选择活动类型</option><option value="0">未分类</option>';   
				for(var i = 0 ; i < arr.length ; i ++){
					html_ += '<option value="' + arr[i].id + '">' + arr[i].name + '</option>'; 
				}
				$("#aem-type").append(html_); 
			}else{
				$("#aem-type").append('<option value="">请选择活动类型</option><option value="0">未分类</option>'); 
			}
				
		},
		
		// 前往活动类型列表页
		aemTypePage : function(){
			window.location.href = aemInfo.path + "/view/ui/aemTypePage"; 
		},
		
		// 前往活动添加页面
		aemInfoAddPage : function(){
			window.location.href = aemInfo.path + "/view/ui/aemInfoAddPage"; 
		},
		
		// 前往编辑页
		aemInfoEditPage : function(o){
			window.location.href = aemInfo.path + "/view/ui/aemInfoEditPage?id=" + $(o).attr("attr") + "&status=" + $(o).attr("attr-status");    
		},
		
		// 活动统计 - 马少华的功能
		aemStatisticPage : function(o){
			window.location.href = aemInfo.path + "/view/ui/activeTotalStatistic?id=" + $(o).attr("attr-id")  + "&name=" + encodeURI(encodeURI($(o).attr("attr-name"))) + "&createTime=" + $(o).attr("attr-ctime"); 
		},
		
		// 活动传播		- 马杰的功能																 
		aemDisseminationPage : function(o){
			window.location.href = aemInfo.path + "/view/ui/disseminationActivities?aemId=" + $(o).attr("attr-id")  + "&aemName=" + encodeURI(encodeURI($(o).attr("attr-name"))); 
		},
		
		// 参与名单       attr-status="' + e.status + '" attr-s="' + e.startTime + '" attr-e="' + e.endTime + '" 
		aemJoinPageList : function(o) {
			window.location.href = aemInfo.path + "/view/ui/aemWechatJoinListPage?id=" + $(o).attr("attr") + "&status=" + $(o).attr("attr-status") + "&startTime=" + $(o).attr("attr-s") + "&endTime=" + $(o).attr("attr-e"); 
		},
		
		// 显示预览一条活动的二维码
		showQrcode : function(e){
			var d = dialog({
				width: 110,
	 	        height:110,
	 	        align: 'bottom',
	 			content: '<div id="code"></div><img id="qrcodeImg" src="" style="width:110px;height:110px;"/>' +
	 	        				'<a id="download" download="qrcode.jpg"></ a></br>' 
//	 	        				'<input id="copy-value" type="text" value="' + $(e).attr("attr") + '" style="width:110px;margin-top:10px"/></br>'+
//	 	        				'<button id="copy-btn" data-clipboard-target="#copy-value" onclick="aemInfo.copyUrl(this)" style="margin-top:8px;width:110px;height:30px;cursor: pointer;" type="button" class="btn btn-primary k_verify">复制链接</button>'
	 							,
				quickClose: true// 点击空白处快速关闭
			});
			d.show(document.getElementById(  $(e).attr("id")  ));
			aemInfo.createQRCode("code", $(e).attr("attr") , 100 , 50 , null); 
			$(".ui-dialog").css("background-color" , "rgba(7, 17, 27, 0.3)");  
		},
		
		
		 // 创建二维码
	    createQRCode : function(id, url, w, h, src){
	    	$('#code').empty();
	    	var qrcode = $('#code').qrcode({
		   			width : 110,
		   			height:110,
		   			text : url + ""    /*可以通过ajax请求动态设置*/
		 	}).attr("style","display:none;");
        	var canvas = qrcode.find('canvas').get(0);  // 将生成的二维码转换成图片格式
	        $('#qrcodeImg').attr('src', canvas.toDataURL('image/jpg'));
		 
	    }, 
	    
	    // 复制预览一条活动的二维码 的真实链接
	    copyUrl : function(o){
	    	var btn = document.getElementById('copy-btn');
	        var clipboard = new ClipboardJS(btn);//实例化
	        // 复制成功执行的回调，可选
	        clipboard.on('success', function(e) { 
//	            console.info('Text:', e.text); 
	        });
	        clipboard.on('error', function(e) {
	            console.error('Action:', e.action); 
	        });
	    },
		
	    
	    // 删除操作
		aemInfoDelete : function(o){
			/*swal({
				  title: '删除确认',
				  text: "删除后将无法恢复，请谨慎操作！",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#d33',
				  cancelButtonColor: '#3085d6',
				  confirmButtonText: '删除',
				  cancelButtonText:'取消'
				}).then(function () {
					var data = {
							id : $(o).attr("attr")
					};
					var url = aemInfo.path + "/aemInfo/ajaxDeleteAemInfo"; 
					var e = JSON.parse(aemInfo.syncAjax("post", url , data , false)); 
					if(e.status == "200"){
						aemInfo.search(); 
					}
				})*/
				
			swal({
				  title: '删除确认',
				  text: "您确定要删除此活动吗? 删除后将无法恢复，请谨慎操作！",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#1ab394',
				  cancelButtonColor: '#E8E8E8',
				  confirmButtonText: '确认',
				  cancelButtonText:'取消',
				  reverseButtons : true
				}).then(function () {
					var data = {
							id : $(o).attr("attr")
					};
					var url = aemInfo.path + "/aemInfo/ajaxDeleteAemInfo"; 
					var e = JSON.parse(aemInfo.syncAjax("post", url , data , false)); 
					if(e.status == "200"){
						location.reload()
					}
				})
		},
		
		
		//显示悬浮框
		showDialog : function(level , clickScore , shareScore , id_){
			var follow = document.getElementById('td-span-' + id_); 
			aemInfo.trDialog = dialog({
				width: 200,
		        height:90,
		        align: 'right',
				content:"<p>分享层级："+level+"</p ><p>点击积分："+clickScore+"</p ><p>分享积分："+shareScore+"</p >",
				quickClose: true// 点击空白处快速关闭
			}); 
			aemInfo.trDialog.show(follow);
		},
		
		// 跳转到【预览任务列表】页面
		showAemH5TaskList : function(e){
			var url = aemInfo.path + "/view/ui/aemShowH5TaskList?cid=" + aemInfo.cid;
			var d = dialog({
				width: 110,
	 	        height:110,
	 	        align: 'bottom',
	 			content: '<div id="code"></div><img id="qrcodeImg" src="" style="width:110px;height:110px;"/>' +
	 	        				'<a id="download" download="qrcode.jpg"></ a></br>' 
//	 	        				'<input id="copy-value" type="text" value="' + url + '" style="width:110px;margin-top:10px"/></br>'+
//	 	        				'<button id="copy-btn" data-clipboard-target="#copy-value" onclick="aemInfo.copyUrl(this)" style="margin-top:8px;width:110px;height:30px;cursor: pointer;" type="button" class="btn btn-primary k_verify">复制链接</button>'
	 							,
				quickClose: true// 点击空白处快速关闭
			});
			d.show(document.getElementById(  $(e).attr("id")  ));
			aemInfo.createQRCode("code", url , 100 , 50 , null); 
			$(".ui-dialog").css("background-color" , "rgba(7, 17, 27, 0.3)");  
//			window.open(aemInfo.path + "/view/ui/aemShowH5TaskList?cid=" + aemInfo.cid);  
		},
		
		
		
		
		
		
		// aemInfo.getPath()
		getPath : function(){ 
//			var parse = aemInfo.parseUrl(window.location.href);  
//			var path = parse.protocol + '://' + parse.host + ':' + parse.port;
//			if("scrm_sps" == parse.segments[0]){
//				path +=  '/' + parse.segments[0];
//			}
			return window.parent.servicePath;
		},
		
		trim : function(str) {
		    return str.replace(/(^\s+$)/g, "");
		},
		
		/**
		 * 发送ajax请求                                
		 * 
		 * type_ : post or get
		 * url_ : url 
		 * data_ : data_
		 * async_ : sync type ,true or false
		 */
		syncAjax : function(type_, url_, data_  , async_) {
			var msg_ = null;
			$.ajax({
					dataType : "text",
					type : type_,
					url : url_,
					data : data_,
					async : async_, 
					success : function(msg) {
							msg_ = msg;
					},
					error : function(msg) {
							msg_ = msg;
					}
			});
			return msg_;
		},
		
		parseUrl : function(url) {
	        var a =  document.createElement('a');
	        a.href = url;
	        return {
	            source: url,
	            protocol: a.protocol.replace(':',''),
	            host: a.hostname,
	            port: a.port,
	            query: a.search,
	            params: (function(){
	                var ret = {},
	                    seg = a.search.replace(/^\?/,'').split('&'),
	                    len = seg.length, i = 0, s;
	                for (;i<len;i++) {
	                    if (!seg[i]) { continue; }
	                    s = seg[i].split('=');
	                    ret[s[0]] = s[1];
	                }
	                return ret;
	            })(),
	            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
	            hash: a.hash.replace('#',''),
	            path: a.pathname.replace(/^([^\/])/,'/$1'),
	            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
	            segments: a.pathname.replace(/^\//,'').split('/')
	        };
	    }
}




















